package com.rem.concurrency.juc.my15Lock;

import cn.hutool.core.util.RandomUtil;

import java.util.concurrent.TimeUnit;

/**
 * 可重入锁
 * 拿到sms的锁也拿到了call的锁
 * 所以执行顺序是 一定是执行完sms再执行call 再让其他线程进入
 *
 * @author Rem
 * @date 2021-03-11
 */

public class SynDemo {

    public static void main(String[] args) {
        Phone phone = new Phone();


        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                phone.sms();
            }, RandomUtil.randomString(4)).start();
        }


    }
}


class Phone {
    public synchronized void sms() {
        System.out.println(Thread.currentThread().getName() + ":==>sms");
        try {
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        call(); // 这里也有锁
    }

    public synchronized void call() {
        System.out.println(Thread.currentThread().getName() + ":==>call");
    }
}